<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card class="box-card">
        <!-- 使用 Tabs 组件完成标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- 使用 Layout 布局绘制头部区域 -->
            <el-row type="flex" justify="space-between" align="middle" class="department-header">
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDepts('')">添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- 主体区域绘制 -->
            <el-tree class="departments-tree" :data="treeData" :props="defaultProps" :default-expand-all="true">
              <template slot-scope="{ data }">
                <el-row type="flex" justify="space-between" style="height: 50px; width: 100%;" align="middle">
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="addDepts(data.id)">添加子部门</el-dropdown-item>
                            <el-dropdown-item @click.native="editDepts(data.id)">编辑部门</el-dropdown-item>
                            <el-dropdown-item v-if="data && !data.children" @click.native="delDepts(data.id)">删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
        <el-dialog
          :title="isEdit? '编辑组织架构':'新增组织架构'"
          :visible.sync="showDialog"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >

          <dept-dialog
            v-if="showDialog"
            :is-edit="isEdit"
            :parent-id="parentId"
            :origin-list="originList"
            @update-list="updateList"
            @close="closeConfirm"
          /> </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
// 导入查询企业部门列表的api
import { getDepartments, delDepartment } from '@/api/departments'
import { tranlistToTreeData } from '@/utils'
// 引入子组件
import DeptDialog from './deptDialog.vue'
export default {
  components: {
    DeptDialog
  },
  data() {
    return {
      activeName: 'first', // 被激活的 Tab 标签页
      // 树形控件数据
      treeData: [

      ],
      // 定义结构显示
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false, // 弹框是否显示
      parentId: '', // 父组件id
      isEdit: false, // 是否是编辑
      originList: [] // 精简后的数据
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 删除部门
    async delDepts(id) {
      const confirmRes = await this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 点击取消,给用户提示
      if (confirmRes === 'cancel') return this.$message('您取消了删除')

      // 点击确定,调用api
      const res = await delDepartment(id)
      // 删除成功后,给用户提示刷新列表
      this.$message(res.message)
      this.getDepartments()
    },

    // 编辑部门
    editDepts(id) {
      this.parentId = id
      this.showDialog = true
      // 点击编辑是需要讲 isEdit设置为true
      this.isEdit = true
    },
    // 隐藏弹窗
    closeConfirm() {
      this.showDialog = false
    },
    // 刷新列表,隐藏弹窗
    updateList() {
      this.showDialog = false
      this.getDepartments()
    },
    // 获取组织架构数据
    async getDepartments() {
      const res = await getDepartments()
      // 精简数据传到子组件
      this.originList = res.data.depts.map(item => ({
        name: item.name,
        id: item.id,
        code: item.code,
        pid: item.pid
      }))
      this.treeData = tranlistToTreeData(res.data.depts)
    },
    handleNodeClick(data) {
      console.log(data)
    },
    // 弹出层显示事件
    addDepts(id) {
      this.parentId = id
      this.showDialog = true
      this.isEdit = false
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
.departments-tree {
  margin: 12px 0px;
}
</style>
